<?php
/**********************************************************
 *	EDP Cache Manager
 *	
 *	This cache manager is designed to accelarate the query
 *	from clients.
 *
 *	Seal Created by 2010-5-28
 **********************************************************/
class EDPCacheManager
{
	
	public function __construct(){
	}

	public function Set( $key, $value, $nameSpace ){
		
		list($cacheObject, $policy) = $this->_GetCacheObject($nameSpace, true);
		
		if( !$cacheObject || $cacheObject == NULL )
			return false;
		
		return $cacheObject->set( $key, $value, NULL, $policy['EXPIRE'] );
		
	}
	
	public function Get( $key, $nameSpace ){
		
		$cacheObject = $this->_GetCacheObject($nameSpace);
		
		if( !$cacheObject || $cacheObject == NULL )
			return false;
		
		return $cacheObject->get( $key );
	}
	
	private function _GetCacheObject( $nameSpace, $policy = NULL ){
		
		global $cacheCluster, $cacheInstance;
		
		if( !isset( $GLOBALS["$nameSpace"] ) )
			return false;
				
		$instance = $GLOBALS["$nameSpace"];
		$cluster = &$cacheCluster[ $instance['CLUSTER'] ];
		if( $cluster['STATUS'] == 'DEAD' ){
			$cluster['OBJECT'] = new Memcache();
			if(@$cluster['OBJECT']->connect( $cluster['HOST'], $cluster['PORT'], 3) == false )
				$cluster['OBJECT'] = false; 
			$cluster['STATUS'] = 'LIVE';
		}
		
		if( $policy === NULL )
			return $cluster['OBJECT'];
		else
			return array( $cluster['OBJECT'], $instance );
	}

}

?>